//
//  CXTabBarViewController.m
//  喜马拉雅FM
//
//  Created by hcios on 2017/8/26.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXTabBarViewController.h"
#import <UIKit/UIKit.h>

#import "CXHomeViewController.h"
#import "ZxFMMyLisitenViewController.h"
#import "CXDiscoverViewController.h"
#import "ZxFMLoginTableViewController.h"

#import "CXNavigationViewController.h"
#import "CXTabBar.h"

#import "CXPlayView.h"

@interface CXTabBarViewController()<CXTabBarDelegate, CXPlayViewDelegate>

@property (nonatomic,strong) CXPlayView *playView;

@property(nonatomic,strong) CXTabBar * customtabBar;

@property (nonatomic,strong) AVPlayer *player;

@property(strong,nonatomic)UIWindow *window;

@end


@implementation CXTabBarViewController



#pragma mark - life cycle method 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 开启一个通知接受,播放URL 及图片
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playingWithInfoDictionary:) name:@"Beginplay" object:nil];
    // 开启两个通知接收(MeViewController传入)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hidePlayView:) name:@"hidePlayView" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPlayView:) name:@"showPlayView" object:nil];
    
    [self performSelector:@selector(createButton) withObject:nil afterDelay:0.1];
    
    
}


- (void)createButton
{
    self.playView = [[CXPlayView alloc] init];
    self.playView.delegate = self;
    self.playView.frame = CGRectMake(0, 0, 65, 70);
    
    _window = [[UIWindow alloc]initWithFrame:CGRectMake((CXScreenWidth - 65) / 2, CXScreenHeight - 70, 65, 70)];
    _window.windowLevel = UIWindowLevelAlert+1;
    
    _window.layer.masksToBounds = YES;
    [_window addSubview:self.playView];
    [_window makeKeyAndVisible];//关键语句,显示window
    
}


// 隐藏图片
- (void)hidePlayView:(NSNotification *)notification
{
    self.playView.hidden = YES;
}

// 显示图片
- (void)showPlayView:(NSNotification *)notification
{
    self.playView.hidden = NO;
}


/** 通过播放地址 和 播放图片 */
- (void)playingWithInfoDictionary:(NSNotification *)notification {
    // 设置背景图
    NSURL *coverURL = notification.userInfo[@"coverURL"] ;
    NSURL *musicURL = notification.userInfo[@"musicURL"];
    [self.playView.contentImageview setImageWithURL:coverURL];
    
    // 支持后台播放
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    // 激活
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    
    // 开始播放
    _player = [AVPlayer playerWithURL:musicURL];
    [_player play];
    NSLog(@"%d",__LINE__);
}

#pragma mark - PlayView的代理方法
- (void)playButtonDidClick:(BOOL)selected {
    // 按钮被点击方法, 判断按钮的selected状态
    if (selected) {
        [_player play];// 继续播放
    } else {
        [_player pause];//暂停播放
    }
}



//当开始滑动时候，就会调用，如果返回YES：可以滑动，否则返回NO
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.viewControllers.count == 1) {
        return NO;
    }
    return YES;
}

- (void)dealloc {
    // 关闭消息中心
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}






#pragma mark - init methods
+(instancetype)tabBarVCWithSubControllwes:(NSArray<UIViewController *> *)theSubControllers
                              tabBarItems:(NSArray<CXTabBarItem *> *)items{
    
    return [[self alloc] initWithSubControllwes:theSubControllers tabBarItems:items];
}

-(instancetype)initWithSubControllwes:(NSArray<UIViewController *> *)theSubControllers
                          tabBarItems:(NSArray<CXTabBarItem *> *)items{
    
    if (self = [super init]) {
        
        [self ds_setupSubController:theSubControllers tabBarItems:items];
    }
    return self;
}

-(void)ds_setupSubController:(NSArray<UIViewController *> *)theSubControllers
                 tabBarItems:(NSArray<CXTabBarItem *> *)items{
    if (!theSubControllers) {
        return;
    }
    
    [self setViewControllers:theSubControllers animated:YES];
    
    
    self.customtabBar.delegate = self;
    self.customtabBar = [CXTabBar tabBarWithFrame:self.tabBar.frame items:items];
    
    self.selectedIndex = 0;
    
    [self setValue:self.customtabBar forKey:@"tabBar"];
}

-(void)tabBar:(CXTabBar *)tabBar tabBarIndex:(NSInteger)index{
    self.selectedIndex = index;
    
}

@end
